home *** CD-ROM | disk | FTP | other *** search
/ PCNet 2006 April / PCnet 2006-06.4.iso / shareware / nmsetup.exe / WebServer / web / upload.php < prev    next >
Encoding:
PHP Script  |  2006-05-01  |  13.0 KB  |  343 lines

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // <!--Copyright (c) 2005 Pure Networks Inc.  All rights reserved.-->
  4. ////////////////////////////////////////////////////////////////////////////////
  5. //
  6. // Build: 3.0.6121.0 (Stable)
  7. // $Revision: #3 $
  8. //
  9. ini_set ( "zlib.output_compression", "Off" );
  10. ini_set ( "max_execution_time", "0" );
  11.  
  12. $sNavLocation = "upload";
  13. require "_session_common.php";
  14. require "_folderutils.php";
  15. ///////////////////////////////////////////////////
  16. // Init some variables
  17. ///////////////////////////////////////////////////
  18. $errorOccurred = 0;
  19. $errorText = "";
  20. $sShare = "";
  21. $sPath = "";
  22. $bMaxSizeError = false;
  23.  
  24.             
  25. ///////////////////////////////////////////////////
  26. // We return to this page with this get value after 
  27. // unsetting the folder session cache since unsetting 
  28. // causes a browser refresh in Mac OSX IE
  29. // We check for the QS value and do nothing but show 
  30. // UI for this edge case since the upload succeeded to 
  31. // get to that point in the first place.
  32. ///////////////////////////////////////////////////
  33. if ($_GET['task'] != "done") 
  34. {
  35.     if (trim($_POST['SHARE']) == "")
  36.     {
  37.         $errorOccurred = 200;
  38.         log_activity("QS check", "failure", return_error_text(200, "", $arErrors));
  39.     }
  40.     else
  41.     {
  42.         $sShare = $_POST['SHARE'];
  43.         log_activity("upload share", "info", $sShare);
  44.     }
  45.  
  46.     if (trim($_POST['PATH']) == "")
  47.     {
  48.         $errorOccurred = 201;
  49.         log_activity("QS check", "failure", return_error_text(201, "", $arErrors));
  50.     }
  51.     else
  52.     {
  53.         $sPath = urlDecodeString($_POST['PATH']);
  54.         log_activity("upload path", "info", $sPath);
  55.     }
  56.  
  57.     if (isset($_GET['errorcode']))
  58.     {
  59.         $errorOccurred = $_GET['errorcode'];
  60.     }
  61.     else
  62.     {
  63.         ///////////////////////////////////////////////////
  64.         // check to ensure upload is enabled.
  65.         ///////////////////////////////////////////////////
  66.         try
  67.         {
  68.             $bUploadEnabled = $nmRaManager->UploadsEnabled;
  69.             if (!$bUploadEnabled)
  70.             {
  71.                 log_activity("upload", "error", return_error_text(327, "", $arErrors));
  72.                 goto('/upload.php?errorcode=327&share=' . $sShare . '&path=' . urlEncodeString($sPath));
  73.                 exit();
  74.             }
  75.         }
  76.         catch (exception $ex)
  77.         {
  78.             log_activity("Attempting nmRaManager->UploadsEnabled", "exception", $ex->getMessage());
  79.             goto('/upload.php?errorcode=307');
  80.             exit();
  81.         }
  82.     }
  83.  
  84.     if ((!isValidString($_FILES['userfile']['size']) && !isValidString($_FILES['userfile']['error']) && !isValidString($_FILES['userfile']['tmp_name'])) ||
  85.             $_FILES['userfile']['size'] > $iMaxFileSize )
  86.     {
  87.         // this is not the preferred way to check upload size limit, but we are not in all cases getting all the post
  88.         // data back when we have someone attmept to upload a file over the limit.  We ARE getting back null values for
  89.         // all these fields, so we key off of this.
  90.         $bMaxSizeError = true;
  91.     }
  92.     // if the above stops being true, we can fall back on a more preferred method of bailing due to files over the max size
  93.     if ($bMaxSizeError)
  94.     {
  95.         $errorOccurred = 322;
  96.         log_activity("file upload size check", "failure", return_error_text(322, "", $arErrors));
  97.     }
  98.  
  99.     if ($errorOccurred == 0)
  100.     {
  101.         ///////////////////////////////////////////////////
  102.         // Need to validate that the share and the path 
  103.         // represent valid shares and paths (someone 
  104.         // could have changed them in the post field).
  105.         ///////////////////////////////////////////////////
  106.         try
  107.         {
  108.             $nmSharedPlace = $nmNetworkLib->OpenShare($sShare);
  109.                 
  110.             if (strncmp($nmSharedPlace->Unc, $sPath, strlen($nmSharedPlace->Unc)) != 0)
  111.             {
  112.                 $errorOccurred = 301;
  113.             }
  114.         }
  115.         catch(com_exception $ex)
  116.         {
  117.             $errorOccurred = 103;
  118.             log_activity("Attempting nmNetworkLib->OpenShare($sShare)", "exception", $ex->getMessage());
  119.         }
  120.  
  121.         ///////////////////////////////////////////////////
  122.         // Check for php error codes
  123.         ///////////////////////////////////////////////////
  124.         if ($_FILES['userfile']['error'] != UPLOAD_ERR_OK)
  125.         {
  126.             $errorOccurred = 320;
  127.             
  128.             $errorcode = $_FILES['userfile']['error'];
  129.             if ($errorcode == UPLOAD_ERR_NO_FILE)
  130.             {
  131.                 $errorOccurred = 321;
  132.             }
  133.             else if (($errorcode == UPLOAD_ERR_INI_SIZE) || ($errorcode == UPLOAD_ERR_FORM_SIZE))
  134.             {
  135.                 $errorOccurred = 322;
  136.             }
  137.             else if ($errorcode == UPLOAD_ERR_PARTIAL)
  138.             {
  139.                 $errorOccurred = 323;
  140.             }
  141.             else
  142.             {
  143.                 $errorOccurred = 326;
  144.             }
  145.         }
  146.     }
  147.  
  148.     if ($errorOccurred == 0)
  149.     {
  150.         $uploaddir = get_cfg_var('upload_tmp_dir');
  151.         $uploadfile = $uploaddir . stripslashes($_FILES['userfile']['name']);
  152.         ///////////////////////////////////////////////////
  153.         // What is the new file name?
  154.         ///////////////////////////////////////////////////
  155.         $newfilename = $sPath . "\\" . stripslashes($_FILES['userfile']['name']);
  156.         ///////////////////////////////////////////////////
  157.         // and the file extension of the file?
  158.         ///////////////////////////////////////////////////
  159.         $sFileExtension = strrchr($newfilename, ".");
  160.         ///////////////////////////////////////////////////
  161.         // is the extension of a disallowed type?
  162.         ///////////////////////////////////////////////////
  163.         if (strpos($sincDisallowedExtensions, strtolower($sFileExtension)) === false)
  164.         {
  165.             // good to go
  166.         }
  167.         else
  168.         {
  169.             $errorOccurred = 324;
  170.         }
  171.         if (!$bReplaceOriginalFileOnUpload)
  172.         {
  173.             ///////////////////////////////////////////////////
  174.             // Does the newfilename exist?  If so, we start to
  175.             // increment through until we find out a filename
  176.             // of the format \\machine\path\filename (x).ext 
  177.             // that does not exist and use that to save
  178.             ///////////////////////////////////////////////////
  179.             if (file_exists($newfilename))
  180.             {
  181.                 $sFilename = substr($newfilename, 0, (strlen($newfilename)-strlen($sFileExtension)));
  182.                 $iFileNumber = 1;
  183.                 $bCheckFileVersions = true;
  184.                 do 
  185.                 {
  186.                     if (!file_exists($sFilename." (".$iFileNumber.")".$sFileExtension))
  187.                     {
  188.                         $newfilename = $sFilename." (".$iFileNumber.")".$sFileExtension;
  189.                         $bCheckFileVersions = false;
  190.                     }
  191.                     $iFileNumber++;
  192.                 } while ($bCheckFileVersions == true);
  193.             }
  194.         }
  195.     }
  196.  
  197.     if ($errorOccurred == 0)
  198.     {
  199.         // is there enough drive space for the file?
  200.         $sFreeDriveSpace = disk_free_space($sPath);
  201.         if (isValidString($sFreeDriveSpace) && !$bMaxSizeError)
  202.         {
  203.             if ($_FILES['userfile']['size'] > $sFreeDriveSpace)
  204.             {
  205.                 $errorOccurred = 329;
  206.                 log_activity("drive space check", "failure", return_error_text(329, "", $arErrors));
  207.             }
  208.         }
  209.     }
  210.  
  211.     if ($errorOccurred == 0)
  212.     {
  213.         if (move_uploaded_file($_FILES['userfile']['tmp_name'],$newfilename) === FALSE)
  214.         {
  215.             $errorOccurred = 325;
  216.             log_activity("upload", "failure", return_error_text(325, "", $arErrors));
  217.         }
  218.         else
  219.         {
  220.             ///////////////////////////////////////////////////
  221.             // we're getting a new file into the shared folder,
  222.             // let's blow away the session vars so the cache
  223.             // won't preclude showing it.
  224.             ///////////////////////////////////////////////////
  225.             unset($_SESSION['currentFolder']);
  226.             unset($_SESSION['currentFolderInfo']);
  227.             unset($_SESSION['currentFolderFiles']);
  228.             log_activity("upload", "success", $newfilename);
  229.             goto('/upload.php?task=done&newfilename=' . urlEncodeString($newfilename));
  230.             session_write_close();
  231.             exit();
  232.         }
  233.     }
  234.     else
  235.     {
  236.         log_activity("upload", "failure", return_error_text($errorOccurred, "", $arErrors));
  237.     }
  238. }
  239. else
  240. {
  241.     ///////////////////////////////////////////////////
  242.     // grab the filename from the QS here due to refresh
  243.     ///////////////////////////////////////////////////
  244.     $newfilename = urlDecodeString($_GET['newfilename']);
  245. }
  246. ?>
  247. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  248. <html>
  249. <head>
  250.     <script type="text/javascript">
  251.         function close_window()
  252.         {
  253.             self.close();
  254.         }
  255.         
  256.         function reloadParent()
  257.         {
  258.             if (window.opener && !window.opener.closed)
  259.             {
  260.                 if (window.opener.childOpen != undefined)
  261.                 {
  262.                     window.opener.location.reload();
  263.                 }
  264.             }
  265.         }
  266.     </script>
  267. <title><?php echo htmlspecialchars(returnPageTitle($sIntroHeadline, $sProductNameInformal, $nmSharedPlace, $sPath, $fileName)); ?></title>
  268. <?php
  269. require "_styles_import.php";
  270. ?>
  271. </head>
  272. <body onload="reloadParent();" style="clip: rect(0px, 0px, 0px, 0px); overflow:hidden;" class="UploadBody">
  273. <!--Copyright (c) 2005 Pure Networks Inc.  All rights reserved.-->
  274. <table width="100%" border="0">
  275. <tr>
  276.     <td valign="top">
  277.         <div class="BlankBgRuler"> </div>
  278.         <!--Main Content Starts Here-->
  279.         <?php
  280.             if ($errorOccurred != 0)
  281.             {
  282.                 // we have an error, so let's just ditch the filename so it won't get  displayed
  283.                 $newfilename = "";
  284.                 if ($errorOccurred != 326)
  285.                 {
  286.                     $errormessage = return_error_text($errorOccurred, "", $arErrors);
  287.                 }
  288.                 else
  289.                 {
  290.                     $errormessage = return_error_text(326, "", $arErrors) . $_FILES['userfile']['error'];
  291.                 }
  292.         ?>
  293.             <div class="UploadTable">
  294.             
  295.                                                     <div class="LoginErrorContain">
  296.                                                         <div class="ErrorImage">
  297.                                                             <div class="ErrorText">
  298.                                                                 An error occurred
  299.                                                             </div>
  300.                                                             <div class="ErrorExplain">
  301.                                                                 <?php echo $errormessage; ?>
  302.                                                                 <br/>
  303.                                                                 (Error Code: <?php echo $errorOccurred; ?>)
  304.                                                             </div>
  305.                                                         </div> <!-- /ErrorImage-->
  306.                                                     </div> <!-- /LoginErrorContain -->
  307.                     <a href="javascript:close_window();" class="UploadCloseButton Button" tabindex="1" href="#" onmouseover="this.className='UploadCloseButton ButtonOver'" onmouseout="this.className='UploadCloseButton Button'">Close</a>
  308.             </div>
  309.         <?php
  310.             }
  311.             else
  312.             {
  313.         ?>
  314.             <div class="UploadSetupTable" id="UploadStep2">
  315.                 <div class="UploadStatusHeader">Your File Upload is Complete</div>
  316.                 <br/>
  317.                 <?php
  318.                 if (isValidString($newfilename))
  319.                 {
  320.                 ?>
  321.                 <div class="UploadedFileName">
  322.                     <div class="UploadGrfxYes"><?php echo(truncate_string(htmlentities(basename($newfilename)), $iUploadedFileNameTruncateLength, "...", "right", true));?></div>
  323.                 </div>
  324.                 <?php
  325.                 }
  326.                 ?>
  327.                 <br/>
  328.                 <br/>                
  329.                 <div class="UploadCloseButton Button" tabindex="1" href="#" onmouseover="this.className='UploadCloseButton ButtonOver'" onmouseout="this.className='UploadCloseButton Button'" onclick="close_window()"><a href="javascript:close_window();">Close</a></div>
  330.             </div>
  331.         <?php        
  332.             }
  333.         ?>
  334.  
  335.             <!-- Main content ends here -->
  336.             <br/>
  337.     </td>
  338.     <td> </td>
  339. </tr>
  340. </table>
  341. </body>
  342. </html>
  343.